var map=new BMap.Map("allmap", {minZoom:13,maxZoom:18,enableMapClick:false});//构造底图时，关闭底图可点功能
map.centerAndZoom(new BMap.Point(113.307649675,23.1200491021),13);
var heatmapOverlay;
function initMap(zoneNumber,cityName) {
    //清除地图覆盖物
    map.clearOverlays();
    map.centerAndZoom(cityName,13);// 初始化地图，设置中心点坐标和地图级别
    map.enableScrollWheelZoom(); // 允许滚轮缩放
    $.ajax({
        url:"/map/heatData",
        data:{"city":zoneNumber},
        dataType:"json",
        type:"post",
        success:function (result) {
            if(result.result.length==0){
                return
            }
            var points = result.result;
            heatmapOverlay = new BMapLib.HeatmapOverlay({"radius":20});
            map.addOverlay(heatmapOverlay);
            heatmapOverlay.setDataSet({data:points,max:8});
            if(!isSupportCanvas()){
                alert('热力图目前只支持有canvas支持的浏览器,您所使用的浏览器不能使用热力图功能~')
            }
            //setGradient();

        }
    })
}
//切换城市
// var city =new BMap.LocalSearch(map,{renderOptions:{map:map,autoViewport:true}}); //地图显示到查询结果处
function search(){
    //清空搜索
    $("#search").val("");
    $('#searchCommunity').html("");
    $('#searchCommunity').css('display','none');
    $('#district').val("");
    $('#risk').val("");
    $("#s2id_risk span:first").html("押品风险");
    $('#startDateStart').val("");
    $('#startDateEnd').val("");
    $('#buildArea').val("");
    $("#s2id_buildArea span:first").html("面积区段");
    $('#loansDuration').val("");
    $("#s2id_loansDuration span:first").html("抵押年限");
    // var s = $("#select2-chosen-1").text();
    // city.search(s); //查找城市
    var zoneNumber = $("#city").val();
    var cityName = $("#city").find("option:selected").text();
    if((!zoneNumber || zoneNumber=="-1") && (!cityName || cityName=="请选择城市")){
        map.centerAndZoom(new BMap.Point(116.404, 39.915),12);
        return;
    }
    initMap(zoneNumber,cityName);
}
//是否显示热力图
// closeHeatmap();
function setGradient(){
    /*格式如下所示:
   {
         0:'rgb(102, 255, 0)',
         .5:'rgb(255, 170, 0)',
         1:'rgb(255, 0, 0)'
   }*/
    var gradient = {};
    var colors = document.querySelectorAll("input[type='color']");
    colors = [].slice.call(colors,0);
    colors.forEach(function(ele){
        gradient[ele.getAttribute("data-key")] = ele.value;
    });
    heatmapOverlay.setOptions({"gradient":gradient});
}
// // 城市列表控件
// map.enableInertialDragging();
// map.enableContinuousZoom();
// var size = new BMap.Size(10, 20);
// map.addControl(new BMap.CityListControl({
//     anchor: BMAP_ANCHOR_TOP_LEFT,
//     offset: size,
//     // 切换城市之间事件
//     // onChangeBefore: function(){
//     //    alert('before');
//     // },
//     // 切换城市之后事件
//     // onChangeAfter:function(){
//     //   alert('after');
//     // }
// }));

//判断浏览区是否支持canvas
function isSupportCanvas(){
    var elem = document.createElement('canvas');
    return !!(elem.getContext && elem.getContext('2d'));
}
//监听地图层级
map.addEventListener('zoomend',scrollFunc);
function scrollFunc(e){
    e=e || window.event;
    var zoom =map.getZoom();
    if(zoom <= 13){
        $('#searchCommunity').css('display','none');
        map.clearOverlays();
        var zoneNumber = $("#city").val();
        var cityName = $('#city').find('option:selected').text();
        if((!zoneNumber || zoneNumber=="-1") && (!cityName || cityName=="请选择城市")){
            map.centerAndZoom(new BMap.Point(116.404, 39.915),12);
            return;
        }
        initMap(zoneNumber,cityName);
    }else if(zoom < 18 && zoom >16){
        var communityName=$("#search").val(),
            city=$('#city').val(),
            district=$('#district').val(),
            risk=$('#risk').val(),
            startDateStarts = $('#startDateStart').val(),
            startDateEnds = $('#startDateEnd').val(),
            buildArea=$('#buildArea').val(),
            loansDuration=$('#loansDuration').val(),
            inputData={
                communityName:communityName,
                city:city,
                district:district,
                risk:risk,
                startDateStarts:startDateStarts,
                startDateEnds:startDateEnds,
                buildArea:buildArea,
                loansDuration:loansDuration
            };
        map.clearOverlays();
        initCharts(inputData)
    }
}






